class Event < ActiveRecord::Base
  has_many :traces
  
  validates_presence_of :title, :client, :description
  validates_format_of :ticketNumber, :with => /[0-9]+/, :on => :save

  # Get latest 10 created events
  def self.getLatestEvents
    @events = Event.find(:all,:order => "created_at DESC", :limit => 15)
  end
  
  def totalTime
    t = 0
    self.getTraces.each do |tr|
      t += tr.minSpent
    end
    return t
  end
  
  def getTraces
    return self.traces.sort { |a,b| b.created_at <=> a.created_at }
  end
    
end
